﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MessageDetail.aspx.cs" Inherits="UI_Mail_MessageDetail" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
  <head id="Head1" runat="server">
    <title></title>
    <link href="../../js/jquery.easyui/themes/default/easyui.css" rel="stylesheet" type="text/css" />
    <link href="../../js/jquery.easyui/themes/icon.css" rel="stylesheet" type="text/css" />
    <script src="../../js/jquery.easyui/jquery.min.js" type="text/javascript"></script>
    <script src="../../js/jquery.easyui/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="../../js/common.js" type="text/javascript"></script>
    <script type="text/javascript" src="http://www.jeasyui.com/easyui/datagrid-detailview.js"></script>
    <style type="text/css">
      .basepanel{
        overflow-x: hidden;
      }
    </style>
    <script type="text/javascript">
      var request = {
        QueryString: function (val) {
          var url = window.location.search;
          var re = new RegExp('' + val + '=([^&?]*)', 'ig');
          return ((url.match(re)) ? (url.match(re)[0].substr(val.length + 1)) : null);
        }
      };

      var eBayUserID = request.QueryString('ebayuserid');
      var orderID = request.QueryString('orderid');
      var msgID = request.QueryString('messageid');

      $(document).ready(function () {
        var msgUrl = orderID ?
          '../../TableHanders.aspx?act=q&tid=8017&type=0&ebayuserid=' + eBayUserID + '&orderid=' + orderID :
          '../../TableHanders.aspx?act=q&tid=8022&type=0&ebayuserid=' + eBayUserID + '&messageid=' + msgID;

       $('#message').datagrid({
         fitColumns: true,
         fit: true,
         singleSelect: true,
         url: msgUrl,
         columns: [[
           { field: 'id', title: 'id', hidden: true },
           {
             field: 'Flag', title: '', width: 30, fixed: true,
             formatter: function (value, row, index) {
               return '<img src="../../Images/flag' + value + '.png" width="20" height="20" />';
             }
           },
           {
             field: 'File', title: '', width: 30, fixed: true,
             formatter: function (value, row, index) {
               var imgPath = value ? 'file.png' : 'unfile.png';
               return '<img src="../../Images/' + imgPath + '" width="16" height="16" />';
             }
           },
           {
             field: 'Read', title: '', width: 30, fixed: true,
             formatter: function (value, row, index) { image = value ? 'read.png' : 'unread.png'; return '<img src="../../Images/' + image + '" width="20" height="20" />'; }
           },
           { field: 'Sender', title: 'From', width: 100, fixed: true },
           { field: 'Subject', title: '主题', width: 200 },
           { field: 'SendToName', title: 'To', width: 100 },
           { field: 'ReceiveDate', title: '发件时间', width: 100 },
           { field: 'Type', title: '类型', width: 100 }
         ]],
         view: detailview,
         onLoadSuccess: function (data) {
           $(this).datagrid('expandRow', 0);
         },
         detailFormatter: function (index, row) {
           return '<div id="ddv-' + index + '" class="ddv" style="padding:5px 0;overflow-y:auto;overflow-x:auto;"></div>';
         },
         onExpandRow: function (index, row) {
           var ddv = $(this).datagrid('getRowDetail', index).find('div#ddv-' + index);
           ddv.panel({
             height: 800,
             border: false,
             cache: false,
             href: './GetMessageDetail.aspx?ebayuserid=' + eBayUserID + '&messageid=' + row['MessageID'] + '&externalmessageid=' + row['ExternalMessageID'] + '&recipientid=' + row['Sender'] + '&index=' + index + '&subject=' + row['Subject'],
             onLoad: function () {
               $('#message').datagrid('fixDetailRowHeight', index);
             }
           });
           $('#message').datagrid('fixDetailRowHeight', index);
         },
         onSelect: function (rowIndex, rowData) {
           $(this).datagrid('expandRow', rowIndex);
         }
       });

       if (orderID) {
         $.getJSON(
           './MailHandler.ashx?act=q&id=0109&ebayuserid=' + eBayUserID + '&orderid=' + orderID,
           function (data) {
             if (data.retVal == 0) {
               if (data.retSrc && data.retSrc.Information && data.retSrc.Information.length > 0) {
                 var info = data.retSrc.Information[0];
                 var date = new Date(info.PaidonDate);
                 $('#shippingtime').text(date.dateAdd('d',1).format('yyyy-MM-dd'));
                 $('#shipmenttype').text(info.PostageService);
                 $('#consultcount').text(info.Quantity);
                 $('#consultdays').text(info.Interval);
                 $('#xday').text(date.dateAdd('d', info.XDay).format('yyyy-MM-dd'));
                 $('#yday').text(date.dateAdd('d', info.YDay).format('yyyy-MM-dd'));
                 $('#zday').text(date.dateAdd('d', info.ZDay).format('yyyy-MM-dd'));
               }
             }
             else {
               alert(data.retMsg);
             }
           });
       }
       else if (msgID) {
         $('#header').css('display', 'none');
       }

       $('#flag').combobox({
         width: 80,
         valueField: 'id',
         textField: 'text',
         data: [
           { id: 'important', text: '重要' }
         ],
         formatter: function (row) {
           var imageFile = undefined;
           switch (row.text) {
             case '重要':
               imageFile = '../../Images/1.gif';
               break;
             default:
               break;
           }
           return '<img class="item-img" src="' + imageFile + '" /><span class="item-text">' + row.text + '</span>';
         },
         onSelect: function (rec) {
           alert(rec.text);
         }
       });

       $('#operation').combobox({
         width: 80,
         valueField: 'id',
         textField: 'text',
         data: [
           { id: 'Receipt', text: '发票' },
           { id: 'Reserve', text: '保留' },
           { id: 'CancelOrder', text: '取消订单' },
           { id: 'Blacklist', text: '黑名单' },
           { id: 'Outside', text: '站外信' },
           { id: 'Repository', text: '加入知识库' }
         ],
         panelHeight: 'auto',
         onSelect: function (rec) {
           if (rec.id == 'Receipt') {
             alert('暂未实现');
           }
           else if (rec.id == 'Reserve') {
             if (orderID) {
               //$.getJSON(
               //  './MailHandler.ashx?ebayuserid=' + eBayUserID + '&orderid=' + orderID,
               //  function(data) {
               //    if (data.retVal == 0) {
               //      alert('保留成功');
               //    }
               //  });
             }
           }
           else if (rec.id == 'CancelOrder') {
           }
           else if (rec.id == 'Blacklist') {
             var url = undefined;
             if (orderID) {
               url = '/MailHandler.ashx?act=u&id=0117&ebayuserid=' + eBayUserID + '&orderid=' + orderID,
               $.getJSON(
                 url,
                 function(data) {
                   if (data.retVal == 0) {
                     alert('加入黑名单成功');
                   }
                   else {
                     alert(data.retMsg);
                   }
                 });
             }
             else if (msgID) {
               url = '/MailHandler.ashx?act=u&id=0118&ebayuserid=' + eBayUserID + '&msgid=' + msgID,
               $.getJSON(
                 url,
                 function(data) {
                   if (data.retVal == 0) {
                     alert('加入黑名单成功');
                   }
                   else {
                     alert(data.retMsg);
                   }
                 });
             }
           }
           else if (rec.id == 'Outside') {
           }
           else if (rec.id == 'Repository') {
             insertRepository();
           }
           $('#operation').combobox('setValue', '更多');
         }
       });

       $('#btnfiling').click(function () {
         var row = $('#message').datagrid('getSelected');
         if (row) {
           $.getJSON(
             './MailHandler.ashx?act=u&id=0103&ebayuserid=' + eBayUserID + '&messageid=' + row.MessageID,
             function (data) {
               if (data.retVal == 0) {
                 alert('归档成功');
               }
               else {
                 alert(data.retMsg);
               }
             });
         }
         else {
           alert('请先选中邮件');
         }
       });

       $('#divResponse').window({
         width: 600,
         height: 380,
         title: '回复',
         modal: true,
         shadow: false,
         closed: true,
         collapsible: false,
         minimizable: false,
         maximizable: false
       });

       $('#btnresponse').click(function () {
         var row = $('#message').datagrid('getSelected');
         if (row) {
           $('#divResponse').window('open');
         } else {
           alert('请先选中邮件');
         }
       });

       $('#dvbtnResponse').click(function () {
         var txt = $('#content').val();
         var row = $('#message').datagrid('getSelected');
         $.post(
           './MailHandler.ashx?act=i&id=0001',
           { 'ebayuserid': eBayUserID, 'originalmsgid': row.MessageID, 'externalmessageid': rows.ExternalMessageID, 'subject': 'Re: ' + row.Subject, 'body': txt, 'recipientid': row.Sender, 'orderid': row.orderID, 'folderid': 8704 },
           function (jdata) {
             var data = $.parseJSON(jdata);
             if (data.retVal == 0) {
               alert('保存成功');
             }
             else {
               alert(data.retMsg);
             }
           });
       });

       $('#dvbtnsave').click(function () {
         var txt = $('#content').val();
         var row = $('#message').datagrid('getSelected');
         $.post(
           './MailHandler.ashx?act=i&id=0001',
           { 'ebayuserid': eBayUserID, 'originalmsgid': row.MessageID, 'externalmessageid': rows.ExternalMessageID, 'subject': 'Re: ' + row.Subject, 'body': txt, 'recipientid': row.Sender, 'orderid': row.orderID, 'folderid': 8448 },
           function (jdata) {
             var data = $.parseJSON(jdata);
             if (data.retVal == 0) {
               alert('保存成功');
             }
             else {
               alert(data.retMsg);
             }
           });
       });

       $('#transmit').click(function () {
       });

       $('#btnDelete').click(function () {
         var row = $('#message').datagrid('getSelected');
         if (row) {
           $.getJSON(
             './MailHandler.ashx?act=d&id=0102&ebayuserid=' + eBayUserID + '&messageid=' + row.MessageID,
             function (data) {
               if (data.retVal == 0) {
                 alert('删除成功');
               }
               else {
                 alert(data.retMsg);
               }
             });
         } else {
           alert('请先选中邮件');
         }
       });

       $('#btnUpgrade').click(function () {
         var row = $('#message').datagrid('getSelected');
         if (row) {
           $.getJSON(
             './MailHandler.ashx?act=u&id=0104&ebayuserid=' + eBayUserID + '&messageid=' + row.MessageID,
             function (data) {
               if (data.retVal == 0) {
                 alert('升级成功');
               } else {
                 alert(data.retMsg);
               }
             });
         } else {
           alert('请先选中邮件');
         }
       });

       $('#tbtemplate').datagrid({
         url: '../../TableHanders.aspx?act=q&tid=8023&type=0',
         fitColumns: true,
         columns: [[
           { field: 'ck', checkbox: true },
           { field: 'Name', title: '模板名称', width: 5 }
         ]],
         onCheck: function (rowIndex, rowData) {
           $('#txttemplate').val(rowData.Content);
         }
       });

       $('#btnjoin').click(function () {
         var rows = $('#tbtemplate').datagrid('getChecked');
         var content = '';
         for (i in rows) {
           content += rows[i].Content;
           content += '\r\n\r\n';
         }
         $('#txttemplate').val(content);
       });

       $('#btnmaintain').click(function () {
         window.location.href = './Template.aspx';
       });

       $('#btnoutput').click(function () {
         var txt = $('#txttemplate').val();
         $.post(
           './MailHandler.ashx?act=q&id=6003',
           { 'eBayUserID': eBayUserID, 'OrderID': orderID, 'Content': txt },
           function (jdata) {
             var data = $.parseJSON(jdata);

             if (data.retVal == 0) {
               $('[name="content"]').val(data.retSrc.Message);
             }
           });
       });
     });

     function insertRepository() {
       var row = $('#message').datagrid('getSelected');
       if (orderID && row) {
         var index = $('#message').datagrid('getRowIndex');
         var question = $('#ddv-' + index).find('[title="content"]').text();
         var answer = $('#ddv-' + index).find('[name="content"]').val();
         var postForm = $('<form action="./KnowledgeDetail.aspx?act=q&ebayuserid=' + eBayUserID + '&orderid=' + orderID + '" method="post" target="_self"><input type="hidden" name="answer" value="' + answer + '"><input type="hidden" name="question" value="' + question + '"><form/>');
         $('body').append(postForm);
         postForm.submit();
         //$.post(
         //  "./KnowledgeDetail.aspx?act=q&ebayuserid=" + eBayUserID + "&orderid=" + orderID,
         //  function (data) { }
         //  );
         //window.location.href = "./KnowledgeDetail.aspx?act=q&ebayuserid=" + eBayUserID + "&orderid=" + orderID;
       }
       else {
         alert('请先选中邮件');
       }
     }
    </script>
  </head>
  <body>
    <div style="width: 80%; height: 100%; float: left; margin: 0 5px 0 0;">
      <div id="dvmessage" style="width:100%;height:100%;">
        <div>
          <a id="btnfiling" class="easyui-linkbutton">归档</a>
          <a id="btnresponse" class="easyui-linkbutton">回复</a>
          <a id="btntransmit" class="easyui-linkbutton" data-options="disabled:true">转发</a>
          <a id="btnDelete" class="easyui-linkbutton">删除</a>
          <a id="move" class="easyui-linkbutton">移动</a>
          <a id="btnUpgrade" class="easyui-linkbutton">升级</a>
          <input id="flag" class="easyui-combobox" />
          <input id="operation" class="easyui-combobox" value="更多" />
        </div>
        <div id="header">
          <table style="table-layout: fixed;width:100%">
            <tr>
              <td style="width:33%">退货需求: <span id="refund" /></td>
              <td style="width:33%">发货时间: <span id="shippingtime" /></td>
            </tr>
            <tr>
              <td><span id="shipmenttype" /></td>
              <td>DT询问次数: <span id="consultcount" /></td>
              <td>询问间隔天数: <span id="consultdays" /></td>
            </tr>
            <tr>
              <td>XDAY: <span id="xday" /></td>
              <td>YDAY: <span id="yday" /></td>
              <td>ZDAY: <span id="zday" /></td>
            </tr>
          </table>
        </div>
        <div style="height:768px;min-height:768px;">
          <table id="message" style="height:768px;"></table>
        </div>
      </div>
    </div>
    <div id="dvpanel" class="easyui-panel" cls="basepanel">
      <div style="margin: 5px 5px 5px 5px;">
        <div>
          <input id="txtbase" name="txtbase" type="text" />
          <a id="btnsearchbase" class="easyui-linkbutton" data-options="iconCls:'icon-search'"></a>
        </div>
        <div>
          <a id="btnbase" class="easyui-linkbutton">知识库</a>
          <a id="btnmaintain" class="easyui-linkbutton">维护</a>
        </div>
        <div id="dvtemplate" class="easyui-panel" style="min-height:200px;overflow-x:hidden">
          <table id="tbtemplate">
          </table>
          <textarea id="txttemplate" style="width:98%;" rows=15></textarea>
        </div>
        <div>
          <a id="btnoutput" class="easyui-linkbutton" data-options="plain:true" ><<</a>
          <a id="btnjoin" class="easyui-linkbutton" style="float:right;">合并</a>
        </div>
      </div>
    </div>
    <div id="divResponse">
      <div>
        <textarea id="content" class="content" style="width:99%;overflow-y: hidden;" rows="20"></textarea>
      </div>
      <div style="float: right;">
        <a id="dvbtnResponse" class="easyui-linkbutton">回复</a>
        <a id="dvbtnsave" class="easyui-linkbutton">保存</a>
        <a id="dvbtnattach" class="easyui-linkbutton" data-options="disabled:'false'">附件</a>
      </div>
    </div>
  </body>
</html>
